package gui.subpanel;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class ColumnControlTableCellHandler {

	private List<ColumnControlPanel> panelList = new ArrayList<ColumnControlPanel>();
	private TableCellRenderer tableCellRenderer = null;
	private TableCellEditor tableCellEditor = null;

	private ActionListener addButtonActionListener = null;
	private ActionListener removeButtonActionListener = null;
	private ActionListener upButtonActionListener = null;
	private ActionListener downButtonActionListener = null;

	private int currentRowIndex = 0;

	/**
	 * Default constructor.
	 */
	public ColumnControlTableCellHandler() {
		super();
		initTableCellRenderer();
		initTableCellEditor();
	}

	private void initTableCellRenderer() {
		tableCellRenderer = new DefaultTableCellRenderer() {
			private static final long serialVersionUID = 1L;

			@Override
			public Component getTableCellRendererComponent(JTable table,
					Object value, boolean isSelected, boolean hasFocus,
					int row, int column) {
				return getColumnControlPanel(row);
			}
		};
	}

	private void initTableCellEditor() {
		tableCellEditor = new TableCellEditor() {
			public boolean stopCellEditing() {
				return true;
			}

			public boolean shouldSelectCell(EventObject anEvent) {
				return true;
			}

			public void removeCellEditorListener(CellEditorListener l) {
			}

			public boolean isCellEditable(EventObject anEvent) {
				return true;
			}

			public Object getCellEditorValue() {
				return "";
			}

			public void cancelCellEditing() {
			}

			public void addCellEditorListener(CellEditorListener l) {
			}

			public Component getTableCellEditorComponent(JTable table,
					Object value, boolean isSelected, int row, int column) {
				currentRowIndex = row;
				return getColumnControlPanel(row);
			}
		};
	}

	public synchronized ColumnControlPanel getColumnControlPanel(int row) {
		ColumnControlPanel panel = null;
		int panelListSize = panelList.size();
		if (row < panelListSize) {
			panel = panelList.get(row);
		} else if (row > panelListSize) {
			throw new IllegalArgumentException();
		} else {
			panel = new ColumnControlPanel();
			panelList.add(panel);
			// bind event
			bindAddButtonActionListener(panel);
			bindRemoveButtonActionListener(panel);
			bindUpButtonActionListener(panel);
			bindDownButtonActionListener(panel);
		}
		return panel;
	}

	// event control
	public void bindAddButtonActionListener(ActionListener actionListener) {
		this.addButtonActionListener = actionListener;
		for (ColumnControlPanel panel : panelList) {
			bindAddButtonActionListener(panel);
		}
	}

	private void bindAddButtonActionListener(ColumnControlPanel panel) {
		if (addButtonActionListener != null) {
			panel.getBtnNewButton().addActionListener(addButtonActionListener);
		}
	}

	public void bindRemoveButtonActionListener(ActionListener actionListener) {
		this.removeButtonActionListener = actionListener;
		for (ColumnControlPanel panel : panelList) {
			bindRemoveButtonActionListener(panel);
		}
	}

	private void bindRemoveButtonActionListener(ColumnControlPanel panel) {
		if (removeButtonActionListener != null) {
			panel.getBtnNewButton_1().addActionListener(
					removeButtonActionListener);
		}
	}

	public void bindUpButtonActionListener(ActionListener actionListener) {
		this.upButtonActionListener = actionListener;
		for (ColumnControlPanel panel : panelList) {
			bindUpButtonActionListener(panel);
		}
	}

	private void bindUpButtonActionListener(ColumnControlPanel panel) {
		if (upButtonActionListener != null) {
			panel.getBtnNewButton_2().addActionListener(upButtonActionListener);
		}
	}

	public void bindDownButtonActionListener(ActionListener actionListener) {
		this.downButtonActionListener = actionListener;
		for (ColumnControlPanel panel : panelList) {
			bindDownButtonActionListener(panel);
		}
	}

	private void bindDownButtonActionListener(ColumnControlPanel panel) {
		if (downButtonActionListener != null) {
			panel.getBtnNewButton_3().addActionListener(
					downButtonActionListener);
		}
	}

	/**
	 * Get Panel List
	 * 
	 * @return List{ColumnControlPanel}
	 */
	public List<ColumnControlPanel> getPanelList() {
		return panelList;
	}

	/**
	 * Set Panel List
	 * 
	 * @param panelList
	 *            List{ColumnControlPanel}
	 */
	public void setPanelList(List<ColumnControlPanel> panelList) {
		this.panelList = panelList;
	}

	/**
	 * Get TableCellRenderer
	 * 
	 * @return TableCellRenderer
	 */
	public TableCellRenderer getTableCellRenderer() {
		return tableCellRenderer;
	}

	/**
	 * Set TableCellRenderer
	 * 
	 * @param tableCellRenderer
	 *            TableCellRenderer
	 */
	public void setTableCellRenderer(TableCellRenderer tableCellRenderer) {
		this.tableCellRenderer = tableCellRenderer;
	}

	/**
	 * Get TableCellEditor
	 * 
	 * @return TableCellEditor
	 */
	public TableCellEditor getTableCellEditor() {
		return tableCellEditor;
	}

	/**
	 * Set TableCellEditor
	 * 
	 * @param tableCellEditor
	 *            TableCellEditor
	 */
	public void setTableCellEditor(TableCellEditor tableCellEditor) {
		this.tableCellEditor = tableCellEditor;
	}

	/**
	 * Get Current Row Index
	 * 
	 * @return int
	 */
	public int getCurrentRowIndex() {
		return currentRowIndex;
	}

	/**
	 * Set Current Row Index
	 * 
	 * @param currentRowIndex
	 *            int
	 */
	public void setCurrentRowIndex(int currentRowIndex) {
		this.currentRowIndex = currentRowIndex;
	}

}